package com.ruoyi.project.wx.service.impl;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.wx.dao.MemberFollowDao;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.MemberFollowDO;
import com.ruoyi.project.wx.service.MemberFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class MemberFollowServiceImpl implements MemberFollowService {
	@Autowired
	private MemberFollowDao memberFollowDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public MemberFollowDO get(String id){
		return memberFollowDao.get(id);
	}
	
	@Override
	public List<MemberFollowDO> list(Map<String, Object> map){
		return memberFollowDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberFollowDao.count(map);
	}
	
	@Override
	public int save(MemberFollowDO memberFollow){
		return memberFollowDao.save(memberFollow);
	}
	
	@Override
	public int update(MemberFollowDO memberFollow){
		return memberFollowDao.update(memberFollow);
	}
	
	@Override
	public int remove(String id){
		return memberFollowDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberFollowDao.batchRemove(ids);
	}

	@Override
	public R setFollow(Map<String, Object> map) {
		List<MemberFollowDO> followDOList = memberFollowDao.list(map);
		if (followDOList.size() != 0) {  //已经关注
			for (MemberFollowDO followDO : followDOList) {
				memberFollowDao.remove(followDO.getId());
			}
			return R.ok("取消关注");
		} else {
			MemberFollowDO followDO = new MemberFollowDO();
			followDO.setId(UuidUtil.get32UUID());
			followDO.setUserId(map.get("userId").toString());
			followDO.setGoodsId(map.get("goodsId").toString());
			followDO.setCreateAt(new Date());
			if (memberFollowDao.save(followDO) > 0) {
				return R.ok("关注成功");
			} else {
				return R.error();
			}
		}
	}

	@Override
	public PageInfo<Map<String,Object>> getFollowList(Map<String, Object> map1) {
		PageHelperUtils.page(map1);
		List<Map<String, Object>> goodsList = memberFollowDao.getFollowList(map1);
		/*if(map1.containsKey("userId")&&StringUtils.isNotBlank(map1.get("userId").toString())&&goodsList.size()>0){
			MemberUserDO memberUserDO = memberUserDao.get(map1.get("userId").toString());
			for (Map<String, Object> map : goodsList) {
				if(memberUserDO.getLevel()=='A'){
					map.put("price", map.get("price_a").toString());
				}else if (memberUserDO.getLevel()=='B'){
					map.put("price", map.get("price_b").toString());
				}else if (memberUserDO.getLevel()=='C'){
					map.put("price", map.get("price_c").toString());
				}
			}
		}*/
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return pageInfo;
	}
	
}
